package com.gzyj.flink.gps.sink.mysql;

import com.gzyj.flink.gps.factory.MySQLMybatisSessionFactory;
import com.gzyj.flink.gps.mapper.mysql.TpvehdataMapper;
import com.gzyj.flink.pojo.gps.Tpvehdata;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class TpvehDataSink extends RichSinkFunction<Tpvehdata> {

    SqlSessionFactory mysql;

    @Override
    public void open(Configuration parameters) throws Exception {
        mysql = MySQLMybatisSessionFactory.getSqlSessionFactory();
    }

    @Override
    public void close() throws Exception {
    }

    @Override
    public void invoke(Tpvehdata value, Context context) throws Exception {
        SqlSession sqlSession = mysql.openSession(true);
        try {
            TpvehdataMapper tpvehdataMapper = sqlSession.getMapper(TpvehdataMapper.class);
            tpvehdataMapper.insertSelective(value);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            sqlSession.close();
        }


    }
}
